<template>
 <div class="member-order">
  <XtxTabs v-model="activeName" @tabClick="clickTab">
    <XtxTabsPanel
    v-for="item in orderStatus"
    :key="item.name"
    :name="item.name"
    :label="item.label"
    >
  </XtxTabsPanel>
  </XtxTabs>

  <div class="order-list" >
      <div class="loading" v-if="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
      v-for="item in orderList"
      :key="item.id" :orderList="item"
      @on-cancel="handlerCancel"
      @on-delete="onDeleteOrder"
      @on-confirm="useConfirmOrder"
      @on-logistics="handlerLogistics"
        />
  </div>

  <!-- 分页组件 -->
  <XtxPagination
  :total="total"
  :pageSize="requestPanel.pageSize"
  :currenPage="requestPanel.page"
  @current-change="requestPanel.page = $event"/>
  <!-- 取消订单原因组件 -->
  <OrderCancel ref="orderCancelCom" />

  <!-- 查看物流组件 -->
  <OrderLogistics ref="detailLogistics"/>
 </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constant'
import { findOrderList, deleteOrder, confirmOrder } from '@/api/order'
import OrderItem from './components/order-item.vue'
import OrderCancel from './components/order-cancel.vue'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
import OrderLogistics from './components/order-logistics.vue'
export default {
  name: 'MemberOrder',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistics
  },
  setup () {
    const activeName = ref('all')
    const orderList = ref(null)
    const loading = ref(false)
    const total = ref(0)
    const requestPanel = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    const clickTab = (tabName) => {
      if (!loading.value) {
        requestPanel.page = 1
        requestPanel.orderState = tabName.index
      }
    }

    const getOrderList = () => {
      loading.value = true
      findOrderList(requestPanel).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }

    const onDeleteOrder = (orderId) => {
      Confirm({ text: '您确认删除这条订单吗？' }).then(() => {
        deleteOrder({ id: orderId }).then(data => {
          Message({ text: '删除成功', type: 'success' })
          getOrderList()
        })
      }).catch(e => {})
    }

    watch(requestPanel, () => {
      if (!loading.value) {
        getOrderList()
      }
    }, { immediate: true })
    return {
      clickTab,
      activeName,
      orderStatus,
      requestPanel,
      orderList,
      loading,
      total,
      onDeleteOrder,
      ...userConfirm(),
      ...useCancel(),
      ...userLogistics()
    }
  }
}
// 取消订单
export const useCancel = () => {
  // 组件实例
  const orderCancelCom = ref(null)
  // 点击取消
  const handlerCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}

// 确认收货
export const userConfirm = () => {
  const useConfirmOrder = (order) => {
    Confirm({ text: '您确认点击收货吗？' }).then(() => {
      confirmOrder({ id: order.id }).then(data => {
        Message({ text: '确认成功', type: 'success' })
        order.orderState = 4
      })
    })
  }
  return { useConfirmOrder }
}

// 查看物流
export const userLogistics = () => {
  const detailLogistics = ref(null)
  const handlerLogistics = (order) => {
    detailLogistics.value.open(order)
  }
  return { detailLogistics, handlerLogistics }
}
</script>

<style scoped lang="less">
.member-order {
  height: 100%;
  background: #fff;
}
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
